<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>WebGL Hello World</title>
  <style>
    body {
      margin: 0;
    }

    canvas {
      width: 100vw;
      height: 100vh;
      display: block;
    }
  </style>
</head>

<body>
  <canvas id="c"></canvas>
</body>
<script id="vertex-shader-2d" type="notjs">
// an attribute will receive data from a bufferData
attribute vec4 a_position;
// all shader have a main function
void main() {
  // gl_Position is a special variable a  vertex shader 
  // is responsible for setting
  gl_Position = a_position;
}

</script>
<script id="fragment-shader-2d" type="notjs">
  // fragment shader dont have a default precision 
  // so we need to pick a one,mediump float
  void main() {
    // gl_FragColor is a special variable a fragment shader 
    // is responsible for setting
    gl_FragColor = vec4(1, 0.5, 0, 1); 
    // return redish-purple
  }
</script>
<script src="./lib/webgl-utils.js"></script>
<script>
  "use strict";
  function createShader(gl, type, source) {
    var shader = gl.createShader(type);
    gl.shaderSource(shader, source);
    gl.compileShader(shader);
    var success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
    if (success) {
      return shader;
    }
    console.log(gl.getShaderInfoLog(shader))
    gl.deleteShader(shader)
  }

  function createProgram(gl, vertexShader, fragmentShader) {
    var program = gl.createProgram();
    gl.attachShader(program, vertexShader);
    gl.attachShader(program, fragmentShader);
    gl.linkProgram(program);
    var success = gl.getProgramParameter(program, gl.LINK_STATUS);
    if (success) {
      return program;
    }
    console.log(gl.getProgramInfoLog(program));
    gl.deleteProgram(program);
  }
  function main() {
    // Get a WebGL context
    var canvas = document.getElementById('c');
    var gl = canvas.getContext('webgl');
    if (!gl) {
      return;
    }
    // Get the strings for our GLSL shaders
    var vertexShaderSource = document.getElementById('vertex-shader-2d').text;
    var fragmentShaderSource = document.getElementById('fragment-shader-2d').text;


    // create GLSL shaders, upload GLSL shader source, compile the shaders
    var vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexShaderSource);
    var fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentShaderSource);

    // Link the two shaders into a program
    var program = createProgram(gl, vertexShader, fragmentShader);

    // look up data where the vertex data needs to go.
    var positionAttributeLocation = gl.getAttribLocation(program, 'a_position');

    // Create a buffer and put three 2d clip space points in it 
    var positionBuffer = gl.createBuffer()

    // Bind it to ARRAY_BUFFER (think of it  as ARRAY_BUFFER = positionBuffer)
    gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);

    var positions = [
      0, 0,
      0, 0.5,
      0.7, 0
    ];
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);


    webglUtils.resizeCanvasToDisplaySize(gl.canvas);

    // Tell WebGL how to convert from clip space to pixels
    gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);

    // Clear canvas
    gl.clearColor(0, 0, 0, 0);
    gl.clear(gl.COLOR_BUFFER_BIT);

    // Tell it to use our program (pair of shader)
    gl.useProgram(program);


    // Turn on the attribute
    gl.enableVertexAttribArray(positionAttributeLocation);

    // Bind the position buffer
    gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);

    // Tell the attribute how to get data out of positionBuffer (ARRAY_BUFFER
    var size = 2;
    var type = gl.FLOAT;
    var normalize = false;
    var stride = 0;
    var offset = 0;
    gl.vertexAttribPointer(positionAttributeLocation, size, type, normalize, stride, offset);

    // Draw

    var primitiveType = gl.TRIANGLES

    var offset = 0;
    var count = 3;
    gl.drawArrays(primitiveType, offset, count);
  }
  window.addEventListener('DOMContentLoaded', main)
</script>

</html>